/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doan2database;

/**
 *
 * @author DongVan
 */
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import com.sun.mail.util.BASE64DecoderStream;
import com.sun.mail.util.BASE64EncoderStream;

public class AESEncrypt {
    private final String key = "ThisIsASecretKey";
	//private Cipher ecipher;
	//private Cipher dcipher;
	
	public AESEncrypt(){
//		byte[] raw = key.getBytes(Charset.forName("UTF-8"));
//		if (raw.length != 16) {
//			throw new IllegalArgumentException("Invalid key size.");
//		}
//		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
//		try {
//			ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//			ecipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
//			//dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//			//dcipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
//		} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
//			e.printStackTrace();
//		} catch (InvalidKeyException e) {
//			e.printStackTrace();
//		} catch (InvalidAlgorithmParameterException e) {
//			e.printStackTrace();
//		}
		
	}
	
	public String encryptString(String value) {

		String result = "";
		byte[] raw = key.getBytes(Charset.forName("UTF-8"));
		if (raw.length != 16) {
			throw new IllegalArgumentException("Invalid key size.");
		}
		
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher;
		try {
			cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
			byte[] text = cipher.doFinal(value.getBytes(Charset.forName("UTF8")));// UTF-8
			text = BASE64EncoderStream.encode(text);
			result = new String(text);
		} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public String decryptString(String value){
		String result = "";
		byte[] encrypted = BASE64DecoderStream.decode(value.getBytes());
		byte[] raw = key.getBytes(Charset.forName("UTF-8"));
		if (raw.length != 16) {
			throw new IllegalArgumentException("Invalid key size.");
		}
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

		Cipher cipher;
		try {
			cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
			byte[] original = cipher.doFinal(encrypted);
			result = new String(original, "UTF8");// UTF-8
		} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return  result;
	}
}
